package com.huawei.hms.framework.network.restclient.hwhttp.cronet;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;

class MessageLoop implements Executor {
    static final /* synthetic */ boolean $assertionsDisabled = (!MessageLoop.class.desiredAssertionStatus());
    private static final long INVALID_THREAD_ID = -1;
    private boolean mLoopFailed;
    private boolean mLoopRunning;
    private InterruptedIOException mPriorInterruptedIOException;
    private RuntimeException mPriorRuntimeException;
    private final BlockingQueue<Runnable> mQueue = new LinkedBlockingQueue();
    private long mThreadId = INVALID_THREAD_ID;

    MessageLoop() {
    }

    private boolean calledOnValidThread() {
        if (this.mThreadId == INVALID_THREAD_ID) {
            this.mThreadId = Thread.currentThread().getId();
            return true;
        } else if (this.mThreadId != Thread.currentThread().getId()) {
            return false;
        } else {
            return true;
        }
    }

    private Runnable take(boolean z, long j) throws InterruptedIOException {
        Runnable runnable;
        if (z) {
            runnable = (Runnable) this.mQueue.poll(j, TimeUnit.NANOSECONDS);
        } else {
            try {
                runnable = (Runnable) this.mQueue.take();
            } catch (Throwable e) {
                InterruptedIOException interruptedIOException = new InterruptedIOException();
                interruptedIOException.initCause(e);
                throw interruptedIOException;
            }
        }
        if (runnable != null) {
            return runnable;
        }
        throw new SocketTimeoutException("loop timeout");
    }

    public void loop() throws IOException {
        loop(0);
    }

    public void loop(int i) throws IOException {
        if ($assertionsDisabled || calledOnValidThread()) {
            long nanoTime = System.nanoTime();
            long convert = TimeUnit.NANOSECONDS.convert((long) i, TimeUnit.MILLISECONDS);
            if (this.mLoopFailed) {
                if (this.mPriorInterruptedIOException != null) {
                    throw this.mPriorInterruptedIOException;
                }
                throw this.mPriorRuntimeException;
            } else if (this.mLoopRunning) {
                throw new IllegalStateException("Cannot run loop when it is already running.");
            } else {
                this.mLoopRunning = true;
                while (this.mLoopRunning) {
                    if (i == 0) {
                        try {
                            take(false, 0).run();
                        } catch (InterruptedIOException e) {
                            this.mLoopRunning = false;
                            this.mLoopFailed = true;
                            this.mPriorInterruptedIOException = e;
                            throw e;
                        } catch (RuntimeException e2) {
                            this.mLoopRunning = false;
                            this.mLoopFailed = true;
                            this.mPriorRuntimeException = e2;
                            throw e2;
                        }
                    }
                    take(true, (convert - System.nanoTime()) + nanoTime).run();
                }
                return;
            }
        }
        throw new AssertionError();
    }

    public void quit() {
        if ($assertionsDisabled || calledOnValidThread()) {
            this.mLoopRunning = false;
            return;
        }
        throw new AssertionError();
    }

    public void execute(Runnable runnable) throws RejectedExecutionException {
        if (runnable == null) {
            throw new IllegalArgumentException();
        }
        try {
            this.mQueue.put(runnable);
        } catch (Throwable e) {
            throw new RejectedExecutionException(e);
        }
    }

    public boolean isRunning() {
        return this.mLoopRunning;
    }

    public boolean hasLoopFailed() {
        return this.mLoopFailed;
    }
}
